package cn.edu.klmy.dao.impl;

import cn.edu.klmy.dao.ITeacherClaszRelDao;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Repository
public class TeacherClaszRelDao extends JdbcDaoSupport implements ITeacherClaszRelDao {

    private static final String ADD_TEACHER_TO_CLASZ = "INSERT TEACHER_CLASSES(TEACHER_ID, CLASS_ID) VALUES(?, ?)";

    @Override
    public void addTeachersToClasz(final int[] teacherIds, final int claszId) {
        getJdbcTemplate().batchUpdate(ADD_TEACHER_TO_CLASZ, new MyBatchPreparedStatementSetter(teacherIds, claszId));
    }

    @Override
    public void addClaszsToTeacher(final int[] claszIds, final int teacherId) {
        getJdbcTemplate().batchUpdate(ADD_TEACHER_TO_CLASZ, new YourBatchPreparedStatementSetter(claszIds, teacherId));
    }

    private static final String REMOVE_TEACHER_TO_CLASZ = "DELETE FROM TEACHER_CLASSES WHERE TEACHER_ID = ? AND CLASS_ID = ?";

    @Override
    public void removeTeachersFromClasz(int[] teacherIds, int claszId) {
        getJdbcTemplate().batchUpdate(REMOVE_TEACHER_TO_CLASZ, new MyBatchPreparedStatementSetter(teacherIds, claszId));
    }

    @Override
    public void removeClaszsFromTeacher(int[] claszIds, int teacherId) {
        getJdbcTemplate().batchUpdate(REMOVE_TEACHER_TO_CLASZ, new YourBatchPreparedStatementSetter(claszIds, teacherId));
    }

    private static final String REMOVE_TEACHER_FROM_ALL_CLASZS = "DELETE FROM TEACHER_CLASSES WHERE TEACHER_ID = ?";

    @Override
    public void removeTeacherFromAllClasz(int teacherId) {
        getJdbcTemplate().update(REMOVE_TEACHER_FROM_ALL_CLASZS, new Object[]{teacherId});
    }

    private static final String REMOVE_ALL_TEACHERS_FROM_CLASZ = "DELETE FROM TEACHER_CLASSES WHERE CLASS_ID = ?";

    @Override
    public void removeAllTeacherFromClasz(int claszId) {
        getJdbcTemplate().update(REMOVE_ALL_TEACHERS_FROM_CLASZ, new Object[]{claszId});
    }

    @Resource(name = "dataSource")
    public void setSuperDataSource(DataSource dataSource) {
        super.setDataSource(dataSource);
    }

    private static class MyBatchPreparedStatementSetter implements BatchPreparedStatementSetter {

        private int[] teacherIds;
        private int claszId;

        public MyBatchPreparedStatementSetter(int[] teacherIds, int claszId) {
            this.teacherIds = teacherIds;
            this.claszId = claszId;
        }

        @Override
        public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
            preparedStatement.setInt(1, teacherIds[i]);
            preparedStatement.setInt(2, claszId);
        }

        @Override
        public int getBatchSize() {
            return teacherIds.length;
        }
    }

    private static class YourBatchPreparedStatementSetter implements BatchPreparedStatementSetter {

        private int[] claszIds;
        private int teacherId;

        public YourBatchPreparedStatementSetter(int[] claszIds, int teacherId) {
            this.claszIds = claszIds;
            this.teacherId = teacherId;
        }

        @Override
        public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
            preparedStatement.setInt(1, teacherId);
            preparedStatement.setInt(2, claszIds[i]);
        }

        @Override
        public int getBatchSize() {
            return claszIds.length;
        }
    }
}
